
/**    
 * <p>Copyright： 东软烟草产品研发团队</p>   
 */

package com.jkl.service.projectNew;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.axis2.AxisFault;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jkl.comm.service.CallWebServiceUtil;
import com.jkl.dao.dept.BSDeptMapper;
import com.jkl.dao.project.PJProjectMapper;
import com.jkl.dao.projectNew.PRojectNewMapper;
import com.jkl.entity.BaseResponseDTO;
import com.jkl.entity.dept.BSDept;
import com.jkl.entity.project.po.PJProject;
import com.jkl.entity.projectNew.po.PRojectNew;
import com.jkl.service.projectNew.inter.IProjectNewService;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

/**
 * <p>
 * 123service
 * <p>
 * <p>
 * 123service
 * <p>
 * <p>
 * 变更说明：ji.k 2018年1月17日 123service
 * <p>
 * Date:2018年10月19日
 * 
 * @author ji.k
 * @version 1.0
 */
@Service
@Slf4j
public class PRojectNewService implements IProjectNewService {

	@Value("${systemconfig.webservice.username}")
	private String userName;
	@Value("${systemconfig.webservice.userpssword}")
	private String userpssword;
	@Autowired
	private PRojectNewMapper pRojectNewMapper;
	@Autowired
	private BSDeptMapper bsDeptMapper;
	@Autowired
	private PJProjectMapper projectMapper;
	@Override
	public PRojectNewMapper getMapper() {
		// TODO Auto-generated method stub
		return pRojectNewMapper;
	}

	@Override
	public List<PRojectNew> queryNewProjectByDeptId(String deptId) {
		BSDept dept = bsDeptMapper.selectByPrimaryKey(Integer.parseInt(deptId));
		PRojectNew pjNew = new PRojectNew();
		if (dept.getLevel() == 1) {
			pjNew.setProvincenum(deptId);
		} else if (dept.getLevel() == 2) {
			pjNew.setCitynum(deptId);
		} else if (dept.getLevel() == 3) {
			pjNew.setCountynum(deptId);
		}
		pjNew.setIsnew(1);

		return pRojectNewMapper.selectByEntity(pjNew);
	}

	@Override
	public void updateIsNewByEntity(PRojectNew projectNew) {
		
		pRojectNewMapper.updateIsNewByEntity(projectNew);
	}

	@Override
	public BaseResponseDTO queryNewProjectByPermitCode(String permitCode) {
		BaseResponseDTO  returnDTO= new BaseResponseDTO();
		Example projectExample = new Example(PJProject.class);
		projectExample.createCriteria().andEqualTo("permitCode",permitCode);
		List<PJProject> pjProjec = projectMapper.selectByExample(projectExample);
		if(!CollectionUtils.isEmpty(pjProjec)){
			returnDTO.setResultCode("10001").setResultMessage("该准许证号已导入,请勿重复导入");
		} else {
			
			String url = "http://115.29.2.80/JianCheWebService/OutJkyService.asmx";
			String nameSpace = "http://tempuri.org/";
			String methodName = "OutSGXKinfo";
			String action = "http://tempuri.org/OutSGXKinfo";
			Map<String, String> map = new HashMap<>();
			map.put("username", userName);
			map.put("userpssword", userpssword);
			map.put("BUILDERLICENCENUM", permitCode);
			PRojectNew projectNew = new PRojectNew();
			try {
				String result =CallWebServiceUtil.callWebServiceByDocument(url, nameSpace, methodName, action, map);
				Document doc = DocumentHelper.parseText(result);
				String result2 = doc.getRootElement().element("OutSGXKinfoResult").getStringValue();
				Document doc2 = DocumentHelper.parseText(result2);
				Element ele = doc2.getRootElement().element("sgxkinfo");
				projectNew.setPrjname(ele.elementText("PRJNAME").trim());
				projectNew.setBuilderlicencenum(ele.elementText("BUILDERLICENCENUM").trim());
				projectNew.setArea(ele.elementText("AREA").trim());
				projectNew.setBuildcorpname(ele.elementText("BUILDCORPNAME").trim());
				projectNew.setDesigncorpname(ele.elementText("DESIGNCORPNAME").trim());
				projectNew.setConscorpname(ele.elementText("CONSCORPNAME").trim());
				projectNew.setSupercorpname(ele.elementText("SUPERCORPNAME").trim());
			} catch (AxisFault | DocumentException e) {
				// TODO Auto-generated catch block
				returnDTO.setResultCode("10002").setResultMessage("查询准许证号信息出错!");
			}
			
//			PRojectNew projectNew = pRojectNewMapper.queryNewProjectByPermitCode(permitCode);
			JSONObject returnData = (JSONObject) JSON.toJSON(projectNew);
			returnDTO.setReturnData(returnData);
		}
		return returnDTO;
	}

}